package gw.com.android.ui.quote.view;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.PopupWindow;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.GTConfig;
import gw.com.android.ui.views.CommonTitleBar;
import www.com.library.view.BtnClickListener;

/**
 * 未登陆时，点击行情弹出该引导页
 *
 * @author jett
 * @since 2017-07-27.
 */
public class LoginGuidePopupWindow {

    public static void show(final Activity activity, View anchorView) {
        if (anchorView == null) {
            return;
        }
        int width = anchorView.getWidth();
        int height = anchorView.getHeight();
        if (width <= 0 || height <= 0) {
            return;
        }
        PopupWindow popupWindow = create(activity, width, height);
        popupWindow.showAsDropDown(anchorView, 0, -height);
    }

    private static PopupWindow create(Activity context, int width, int height) {
        View root = View.inflate(context, R.layout.dialog_login_guide, null);
        PopupWindow popupWindow = new PopupWindow(root, width, height);
        initView(context, root, popupWindow);

        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
        return popupWindow;
    }

    private static void initView(final Activity activity, View root, final PopupWindow popupWindow) {
        CommonTitleBar titleBar = (CommonTitleBar) root.findViewById(R.id.common_title_bar);
        titleBar.setLeftIcon(0);
        titleBar.setAppTitle(R.string.main_menu_item_quote);
        titleBar.setRightIcon(R.mipmap.a_nav_close);
        root.findViewById(R.id.btn_to_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GTConfig.instance().setAccountType(GTConfig.ACC_TYPE_REAL);
                ActivityManager.backLogin(activity, false);
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        titleBar.setBtnClickListener(new BtnClickListener() {
            @Override
            public void onBtnClick(int viewID) {
                if (viewID == R.id.title_right_icon) {
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }
                }
            }
        });
    }

}
